package com.ganyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ganyu.domain.ResponseResult;
import com.ganyu.domain.entity.Link;
import com.ganyu.domain.vo.AdminAddLinkVo;
import com.ganyu.domain.vo.AdminLinkVo;
import com.ganyu.domain.vo.LinkListVo;
import com.ganyu.domain.vo.PageVo;
import com.ganyu.mapper.LinkMapper;
import com.ganyu.service.LinkService;
import com.ganyu.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ganyu.constant.SystemConstant.LINK_STATUS_PASS;

/**
 * DATA: 2022/8/30
 * Author: 晨夕
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Autowired
    private LinkService linkService;

    @Autowired
    private LinkMapper linkMapper;
    /**
     * 友链
     */
    @Override
    public ResponseResult<LinkListVo> getAllLink() {
        //1. 在页面查询出所有审核通过的友链
        LambdaQueryWrapper<Link> linkQuery = new LambdaQueryWrapper<>();
        linkQuery.eq(Link::getStatus,LINK_STATUS_PASS);
        List<Link> linkList = linkService.list(linkQuery);
        //2. 返回需要返回的类型（bean拷贝）
        List<LinkListVo> linkListVos = BeanCopyUtils.copyBeanList(linkList, LinkListVo.class);
        return ResponseResult.okResult(linkListVos);
    }

    //获取友链列表
    @Override
    public ResponseResult getLinkList(Integer pageNum , Integer pageSize,String name,String status) {
        List<AdminLinkVo> linkList = linkMapper.getLinkList((pageNum - 1) * pageSize, pageSize, name, status);
        int count = linkMapper.getCount(name, status);
        PageVo<AdminLinkVo> page = new PageVo<>();
        page.setRows(linkList);
        page.setTotal(count);
        return ResponseResult.okResult(page);
    }
    //添加友链
    @Override
    public ResponseResult addLinkList(AdminAddLinkVo adminAddLinkVo) {
        linkMapper.insertLink(adminAddLinkVo);
        return ResponseResult.okResult();
    }

    //根据id查询友链
    @Override
    public ResponseResult getLinkById(Long id) {
        Link link = linkMapper.selectById(id);
        AdminLinkVo adminLinkVo = BeanCopyUtils.copyBean(link, AdminLinkVo.class);
        return ResponseResult.okResult(adminLinkVo);
    }

    //修改友链
    @Override
    public ResponseResult updateLink(AdminLinkVo adminLinkVo) {
        linkMapper.updateLink(adminLinkVo);
        return ResponseResult.okResult();
    }

    //删除友链
    @Override
    public ResponseResult deleteLink(Long[] ids) {
        linkMapper.deleteLinkById(ids);
        return ResponseResult.okResult();
    }
}
